<template>
  <!--库存列表  唐美莉 -->
  <section>
    <table border="0" width="100%" style="border-spacing: 0;border-collapse: collapse;">
      <tr>
        <td>
          <el-form :inline="true">
            <el-form-item>
              <el-input v-model="form.vagueKey" clearable  placeholder="条形码/药品名称"></el-input>
            </el-form-item>
            <el-form-item>
              <el-select v-model="form.supplierId" clearable  placeholder="请选择供应商">
                <!-- <el-option key="-1" value="" label="全部"></el-option> -->
                <el-option
                  v-for="(item,index) in supplierList"
                  :key="index"
                  :value="item.id"
                  :label="item.supplierName"
                ></el-option>
              </el-select>
            </el-form-item>
            <!-- <el-form-item>
              <el-select v-model="form.drugType" placeholder="请选择类型">
                <el-option key="-1" value="" label="全部"></el-option>
                <el-option key="1" value="1" label="西/成药"></el-option>
                <el-option key="2" value="2" label="中药"></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-select v-model="form.numType" clearable placeholder="请选择类型">
                <!-- <el-option key="-1" value="" label="全部"></el-option> -->
                <el-option key="1" value="1" label="大于0"></el-option>
                <el-option key="2" value="2" label="等于0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" circle icon="el-icon-search" @click="getData(-1)"></el-button>
            </el-form-item>
          </el-form>
        </td>
        <td style="text-align:right;vertical-align: top;">
          <el-button @click="exportExcel()" plain>导出</el-button>
        </td>
      </tr>
    </table>
    <table class="table-box">
      <thead>
        <tr>
          <th  style="width:400px;">商品名称</th>
          <th>规格</th>
          <th>单位</th>
          <th>生产厂商</th>
          <th>供应商</th>
          <th>零售价</th>
          <th>成本价</th>
          <th>批号</th>
          <th>有效期</th>
          <th>剩余数量</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td  style="width:400px;">{{item.goodsName}}</td>
          <td>{{item.showNum}}</td>
          <td>{{item.residueNumUnit | changeAssist(dwList) }}</td>
          <td>{{item.factory}}</td>
          <td>{{item.supplierName}}</td>
          <td>￥{{item.retailPrice}}</td>
          <td>￥{{item.purchasePrice}}</td>
          <td>{{item.lotNumber}}</td>
          <td>{{item.effectiveTime | changeTime}}</td>
          <td>
               {{item | residueDurg(dwList)}}
          </td>
          <td><el-link @click="editDurg(item)" :underline="false" type="primary">修改</el-link></td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <edit v-model="editStock" :curItem="curItem" @success="getData"></edit>
    <!-- <set v-model="dialogEdit" :curItem="curItem" @success="getData"></set> -->
  </section>
</template>
<script>
import Excel from "@/_js/excel.js";
export default {
  name: "repertoryList",
  data() {
    return {
      dialogJoin: false,
      dialogEdit: false,
      curItem: null,
      pager: { pageSize: 10, pageNum: 1, total: 0 },
      tableList: [],
      form: {
        source:2,
        numType:'',
        type:2,
        vagueKey: "",
        supplierId: ""
      },
      dwList:[],
      typeList: [],
      qxBtns: {},
      supplierList: [],
      dataList: [],
      distype: "2",
      setType: "1",
      editStock:false
    };
  },
  created() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
    th.getBasicData();
    th.getQX();
    th.getData();
  },
  methods: {
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = {source:2};
      j=Object.assign(j, th.pager);
      j=Object.assign(j, th.form);
      th.showLoading();
      th.$api.pageStockBatchV2(j).then(res => {
        if (res.executed) {
          if (res.dataBody != undefined) {
            th.tableList = res.dataBody.list;
            th.pager.total = res.dataBody.total;
          } else {
            th.tableList = [];
            th.pager.total = 0;
          }
        }
        th.showLoading(false);
      });
    },
    getBasicData() {
      let th = this;
      th.$api.findHpSupplierAll({ pageNum: 1, pageSize: 1000 }).then(res => {
        if (res.executed) {
          th.supplierList = res.dataBody.list;
        }
      });
      th.getCommonConfigByKey("单位").then(res => {
        th.dwList = res;
      });
    },
    exportExcel() {
      let th = this;
      let j = {source:2};
      j = Object.assign(j, th.pager);
      j = Object.assign(j, th.form);
      j.pageSize='1000'
      th.showLoading();
      th.$api.pageStockBatchV2(j).then(res => {
        if (res.executed) {
          if (res.dataBody != undefined) {
            let data = res.dataBody.list;
            let dw = [];
            th.dwList.forEach((item, index) => {
              dw[item.id] = item.desc;
            });
            data.map((it)=>{
              it.currentNum=th.$utils.surplusDurg(it,th.dwList)
            })
            Excel(
              data,
              [
                { key: "goodsName", name: "商品名称" },
                { key: "specs", name: "规格" },
                { key: "residueNumUnit", name: "单位", enum: dw, type: "enum" },
                { key: "factory", name: "生产厂商" },
                { key: "supplierName", name: "供应商" },
                { key: "retailPrice", name: "零售价" },
                { key: "purchasePrice", name: "成本价" },
                { key: "lotNumber", name: "批号" },
                { key: "effectiveTime", name: "有效期" },
                { key: "currentNum", name: "剩余数量" }
              ],
              "物资库存列表"
            );
          }
        }
        th.showLoading(false);
      });
    },
    editDurg(obj){
      const th=this;
      th.editStock=true;
      th.curItem=obj;
    }
  },
  components: {
    "edit":()=>import("../_com/editStock")
  }
};
</script>
